package com.evaluation.controller;

import com.alibaba.fastjson.JSONObject;
import com.evaluation.entity.CommentEntity;
import com.evaluation.entity.StudentEntity;
import com.evaluation.entity.StudentEntityExample;
import com.evaluation.mapper.StudentEntityMapper;
import com.evaluation.utils.Layui;
import com.evaluation.utils.Message;
import com.evaluation.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * 学生管理控制器
 * @author: mty
 * @date: 2021/09/02 15:40
 * @Description:
 */
@RestController
@RequestMapping("/student")
public class StudentController {
    @Autowired
    private StudentEntityMapper studentEntityMapper;

    /**
     * 添加学生操作
     * @param student
     * @return
     */
    @ResponseBody
    @PostMapping("/add")
    public Message add(@RequestBody StudentEntity student) {
        if(StringUtils.isEmpty(student.getLoginName())){
            return Message.error("请填写登录名称");
        }
        if(StringUtils.isEmpty(student.getLoginPw())){
            return Message.error("请填写登录密码");
        }
        if(StringUtils.isEmpty(student.getMobile())){
            return Message.error("请填写手机号");
        }
        if(StringUtils.isEmpty(student.getEmail())){
            return Message.error("请填写邮箱");
        }
        if(StringUtils.isEmpty(student.getSubjectName())){
            return Message.error("请填写专业");
        }
        StudentEntityExample studentEntityExample = new StudentEntityExample();
        studentEntityExample.or().andLoginNameEqualTo(student.getLoginName());
        StudentEntity studentEntity = studentEntityMapper.selectOneByExample(studentEntityExample);
        if(studentEntity!=null){
            return Message.error("登录名称已存在");
        }
        Integer maxId=0;
        if(studentEntityMapper.findMaxId()==null){
            maxId=1;
        }
        student.setStuXuehao(StringUtil.gneerateSn(maxId+1));
        if(studentEntityMapper.insert(student)<=0){
            return Message.error("学生添加失败");
        }
        return Message.success("学生添加成功");
    }

    /**
     * 删除学生操作
     * @param student
     * @return
     */
    @ResponseBody
    @PostMapping("/delete")
    public Message delete(@RequestBody StudentEntity student) {
        try{
            studentEntityMapper.deleteByPrimaryKey(student.getStuId());
        }catch (Exception e){
            return Message.error("删除失败");
        }
        return Message.success("删除成功");
    }

    /**
     * 编辑学生操作
     * @param student
     * @return
     */
    @ResponseBody
    @PostMapping("/edit")
    public Message edit(@RequestBody StudentEntity student) {
        if(StringUtils.isEmpty(student.getLoginName())){
            return Message.error("请填写登录名称");
        }
        if(StringUtils.isEmpty(student.getLoginPw())){
            return Message.error("请填写登录密码");
        }
        if(StringUtils.isEmpty(student.getMobile())){
            return Message.error("请填写手机号");
        }
        if(StringUtils.isEmpty(student.getEmail())){
            return Message.error("请填写邮箱");
        }
        if(StringUtils.isEmpty(student.getSubjectName())){
            return Message.error("请填写专业");
        }
        StudentEntityExample studentEntityExample = new StudentEntityExample();
        studentEntityExample.or().andLoginNameEqualTo(student.getLoginName());
        StudentEntity studentEntity = studentEntityMapper.selectOneByExample(studentEntityExample);
        if(studentEntity!=null){
            if(!student.getStuId().equals(studentEntity.getStuId())){
                return Message.error("登录名称已存在");
            }
        }
        if(studentEntityMapper.updateByPrimaryKey(student)<=0){
            return Message.error("学生编辑失败");
        }
        return Message.success("学生编辑成功");
    }


    @RequestMapping("/select")
    public Layui select(@RequestParam(required = false) String stuRealname, @RequestParam(value = "page") Integer page,
                        @RequestParam(value = "limit") Integer limit) {
        StudentEntityExample example = new StudentEntityExample();
        if (!StringUtils.isEmpty(stuRealname)){
            example.or().andStuRealnameLike("%"+stuRealname+"%");
        }
        Long cou = studentEntityMapper.countByExample(example);
        if(page!=1){
            page=(page-1)*10;
        }else{
            page=page-1;
        }
        return Layui.data(cou.intValue(), studentEntityMapper.selectByExamplePaging(example, page, limit));
    }

    @RequestMapping("/getStudent")
    public StudentEntity getAdmin(@RequestParam(value="stuId")Integer stuId) {
        return studentEntityMapper.selectByPrimaryKey(stuId);
    }


}
